﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SalaDeEnsayo.ApplicationService;

namespace SalaDeEnsayo.WebApplication.SecurityAttributes
{
    public class FilteringSiteMapProvider : XmlSiteMapProvider
    {
        public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
        {
            var isAuthorized = node.Roles.Cast<string>().Any(rol =>
                rol == "*"
                || (rol == "#" && context.User.Identity.IsAuthenticated)
                || context.User.IsInRole(rol));

            if (context.User as SalaDeEnsayoPrincipal != null)
                isAuthorized = isAuthorized && ((SalaDeEnsayoPrincipal)context.User).Activo;

            return isAuthorized;
        }
    }
}
